package org.cardoso.web.jsf;

import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public abstract class ManagedBeanBase implements Serializable {

	private static final long serialVersionUID = -1L;
	
	public static final int SEVERITY_INFO = 1;
	public static final int SEVERITY_WARN = 2;
	public static final int SEVERITY_ERROR = 3;
	public static final int SEVERITY_FATAL = 4;

	public ManagedBeanBase() {
	}
	
	public abstract void initComponents();
	
	public abstract void save(ActionEvent event);
	
	public abstract void remove(ActionEvent event);
	
	public abstract void list();
	
	public abstract Long count();
	
	public abstract void find(Integer firstResult, Integer maxResult);
	
	protected FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}

	protected ExternalContext getExternalContext() {
		return getFacesContext().getExternalContext();
	}

	protected HttpServletRequest getRequest() {
		return (HttpServletRequest) getExternalContext().getRequest();
	}

	protected HttpServletResponse getResponse() {
		return (HttpServletResponse) getExternalContext().getResponse();
	}

	protected void sendRedirect(String url) throws IOException {
		getResponse().sendRedirect(url);
	}

	protected Locale getRequestLocale() {
		return getRequest().getLocale();
	}

	protected Flash getFlashScope() {
		return FacesContext.getCurrentInstance().getExternalContext().getFlash();
	}
	
	protected String getRequestParameter(String name) {
		return getExternalContext().getRequestParameterMap().get(name);
	}

	protected Object getRequestAttribute(String name) {
		return getExternalContext().getRequestMap().get(name);
	}

	protected void setRequestAttribute(String name, Object value) {
		getRequest().setAttribute(name, value);
	}
	
	protected HttpSession getSession() {
		return (HttpSession) getExternalContext().getSession(false);
	}

	protected void setSessionAttribute(String name, Object value) {
		getSession().setAttribute(name, value);
	}

	protected Object getSessionAttribute(String name) {
		return getSession().getAttribute(name);
	}

	protected void removeSessionAttribute(String nome) {
		getSession().removeAttribute(nome);
	}
	
	protected void addMessage(String clienteId, String msg) {
		FacesMessage message = new FacesMessage(msg);
		getFacesContext().addMessage(clienteId, message);
	}
	
	protected void addMessage(String message, int type) {
		switch (type) {
			case SEVERITY_INFO:
				getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
				break;
			case SEVERITY_WARN:
				getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, message, null));
				break;
			case SEVERITY_ERROR:
				getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
				break;
			case SEVERITY_FATAL:
				getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, message, null));
				break;	
			default:
				break;
		}	
	}

}